/*
    Contact info:
    bhepple@freeshell.org
    http://bhepple.freeshell.org/scrmgr

    Copyright (C) 1991-2008 Bob Hepple

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; see the file COPYING.  If not, write to
    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.

*/
#define SM_CLASS extern
#include "smlib.h"

typedef enum { T_SYMBOL, T_STRING, T_START, T_END, T_EOF } token;

token gettoken ARG1(char *, word)
{
    enum {NEUTRAL, INQUOTE, INSYMBOL } state = NEUTRAL;
    int		c;
    char	*w;

    w = word;
    while ((c = getchar()) != EOF) {
	switch (state) {
	case NEUTRAL:
	    switch (c) {
	    case '(':
		return(T_START);
	    case ')':
		return(T_END);
	    case '\n':
	    case ' ':
	    case '\t':
		continue;
	    case '"':
		state = INQUOTE;
		continue;
	    default:
		state = INSYMBOL;
		*w++ = c;
		continue;
	    }
	case INQUOTE:
	    switch (c) {
		char d;

	    case '\\':
		d = getchar();
		switch (d) {
		case 'n': *w++ = '\n'; 	continue;
		case 't': *w++ = '\t'; 	continue;
		case '\n':		continue;
		default:  *w++ = d;	continue;
		}
	    case '"':
		*w = 0;
		return(T_STRING);
	    default:
		*w++ = c;
		continue;
	    }
	case INSYMBOL:
	    switch (c) {
	    case ' ':
	    case '\t':
	    case '\n':
	    case ')':
		ungetc(c, stdin);
		*w = 0;
		return(T_SYMBOL);
	    default:
		*w++ = c;
		continue;
	    }
	}
    }
    return(T_EOF);
}

main()
{
    token	t;
    char	word[200];

    while ((t = gettoken(word)) != T_EOF) {
	switch (t) {
/*
	case T_START: 	printf("T_START "); 			break;
	case T_END:	printf("T_END\n"); 			break;
	case T_SYMBOL:	printf("T_SYMBOL %s ", word); 		break;
*/
	case T_START: 	printf("("); 				break;
	case T_END:	printf(")\n"); 				break;
	case T_SYMBOL:	printf("%s ", word); 			break;
	case T_STRING:	printf("\"%s\" ", word); 		break;
	case T_EOF:	printf("T_EOF\n"); 			break;
	default: 	printf("HUH?\n"); 			break;
	}
    }
}

/* For emacs:
 * Local Variables:
 * tab-width: 8
 * End:
 */
